package Array.Medium;

public class LC0189 {
    public void rotate(int[] nums, int k) {
        int N = nums.length;
        if (N <= 1 || k == 0) return;
        if (k >= N) k = k % N;

        int rightStart = N - 1 - k + 1; // 右半段的起始位置
        reverse(nums, rightStart, N - 1);
        reverse(nums, 0, rightStart - 1);
        reverse(nums, 0, N - 1);
    }

    /**
     * 原地将数组逆序，范围为[start, end]（包含左右边界）
     */
    private void reverse(int[] array, int start, int end) {
        for (int i = start; i <= (start + end) / 2; i++) {
            int tmp = array[i];
            array[i] = array[start + end - i];
            array[start + end - i] = tmp;
        }
    }
}
